import request from '@/utils/request'

// 仪表板统计数据接口
export interface DashboardStats {
  dailyCost: number // 总消耗
  totalImpressions: number // 总曝光
  totalClicks: number // 总点击
  clickRate: number // 点击率
  totalConversions: number // 总转化数
  avgConversionCost: number // 平均转化成本
  potentialCustomers: number // 意向客户数
  dayAvgCost: string // 日均消耗
  cpm: string // CPM
  cpc: string // CPC
  clickPercent: string // 点击率环比
  transferPercent: string // 转化率
  avgTarget: string // 平均转化成本目标
  avgCustomer: string // 意向客户转化率
  wechat_num_ing: number // 在投放微信用户数
  new_wechat_num: number // 新增微信投放用户数
  account_num: number // 广告主体数量
  ad_account_num: number // 账户数量
  ad_group_num: number // 广告组数量
  creative_num: number // 创意数量
}

// 概览数据接口
export interface OverviewStats {
  wechatUsers: number // 在投放微信用户数
  dailyNewUsers: number // 日新增微信投放用户数
  advertiserCount: number // 投放的广告主体数量
  accountCount: number // 账户数量
  adGroupCount: number // 广告组数量
  creativeCount: number // 创意数量
  activeWechatUsers: string // 活跃微信用户数
  dailyUserGrowth: string // 日用户增长率
  activeAdvertiserCount: string // 活跃广告主体数
  activeAccountCount: string // 活跃账户数
  todayActiveAdGroups: string // 今日活跃广告组数
  highPerformanceCreatives: string // 高效创意数
}

// 图表数据接口
export interface ChartData {
  dates: string[] // 日期数组
  costData: number[] // 消耗数据
  conversionData: number[] // 转化数据
  clickRateData: number[] // 点击率数据
  conversionRateData: number[] // 转化率数据
}

// 获取仪表板统计数据
export const getDashboardStats = (params?: {
  advertiserId?: number
  accountId?: number
  days?: string
}) => {
  return request.get({ 
    url: '/dashboard.dashboard/dataCardList', 
    params 
  })
}

// 获取概览统计数据
export const getOverviewStats = (params?: {
  advertiserId?: number
  accountId?: number
  days?: string
}) => {
  return request.get({ 
    url: '/dashboard.dashboard/overviewStats', 
    params 
  })
}

// 获取图表数据
export const getDashboardChartData = (params?: {
  advertiserId?: number
  accountId?: number
  days?: string
}) => {
  return request.get({ 
    url: '/dashboard.dashboard/costEcharts', 
    params 
  })
}


// Mock数据服务（用于演示，实际项目中应该删除）
const mockStats: DashboardStats = {
  dailyCost: 125680,
  totalImpressions: 2856420,
  totalClicks: 45230,
  clickRate: 1.58,
  totalConversions: 1280,
  avgConversionCost: 98.19,
  potentialCustomers: 856,
  dayAvgCost: '￥18,097',
  cpm: '￥44.0',
  cpc: '￥2.78',
  clickPercent: '+12.5%',
  transferPercent: '2.83%',
  avgTarget: '<￥120',
  avgCustomer: '66.9%',
  wechat_num_ing: 125680,
  new_wechat_num: 1856,
  account_num: 68,
  ad_account_num: 152,
  ad_group_num: 896,
  creative_num: 3247
}

const mockOverviewStats: OverviewStats = {
  wechatUsers: 125680,
  dailyNewUsers: 1856,
  advertiserCount: 68,
  accountCount: 152,
  adGroupCount: 896,
  creativeCount: 3247,
  activeWechatUsers: '98,456',
  dailyUserGrowth: '+12.8%',
  activeAdvertiserCount: '56',
  activeAccountCount: '127',
  todayActiveAdGroups: '234',
  highPerformanceCreatives: '1,852'
}

const mockChartData: ChartData = {
  dates: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  costData: [8200, 9320, 9010, 9340, 12900, 13300, 13200],
  conversionData: [1000, 1200, 1100, 1300, 1800, 2000, 1900],
  clickRateData: [1.2, 1.5, 1.8, 1.4, 2.1, 1.9, 1.6],
  conversionRateData: [0.8, 1.1, 1.3, 0.9, 1.6, 1.4, 1.2]
}

// 模拟延时函数
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

// Mock API实现（实际项目中应该删除，使用真实API）
export const getMockDashboardStats = async (params?: {
  advertiserId?: number
  accountId?: number
  days?: string
}): Promise<{ code: number; data: DashboardStats; message: string }> => {
  await delay(800) // 模拟网络延时
  
  // 根据参数调整数据（模拟不同条件下的数据变化）
  const adjustedStats = { ...mockStats }
  
  if (params?.days === '30') {
    adjustedStats.dailyCost = Math.floor(mockStats.dailyCost * 1.5)
    adjustedStats.totalImpressions = Math.floor(mockStats.totalImpressions * 1.8)
    adjustedStats.totalClicks = Math.floor(mockStats.totalClicks * 1.6)
  } else if (params?.days === '1') {
    adjustedStats.dailyCost = Math.floor(mockStats.dailyCost * 0.3)
    adjustedStats.totalImpressions = Math.floor(mockStats.totalImpressions * 0.2)
    adjustedStats.totalClicks = Math.floor(mockStats.totalClicks * 0.25)
  }
  
  return {
    code: 1,
    data: adjustedStats,
    message: '获取成功'
  }
}

// Mock概览数据API
export const getMockOverviewStats = async (params?: {
  advertiserId?: number
  accountId?: number
  days?: string
}): Promise<{ code: number; data: OverviewStats; message: string }> => {
  await delay(600) // 模拟网络延时
  
  // 根据参数调整数据
  const adjustedStats = { ...mockOverviewStats }
  
  if (params?.days === '30') {
    adjustedStats.wechatUsers = Math.floor(mockOverviewStats.wechatUsers * 1.3)
    adjustedStats.dailyNewUsers = Math.floor(mockOverviewStats.dailyNewUsers * 1.2)
    adjustedStats.adGroupCount = Math.floor(mockOverviewStats.adGroupCount * 1.4)
  } else if (params?.days === '1') {
    adjustedStats.dailyNewUsers = Math.floor(mockOverviewStats.dailyNewUsers * 0.8)
    adjustedStats.todayActiveAdGroups = Math.floor(parseInt(mockOverviewStats.todayActiveAdGroups) * 0.6).toString()
  }
  
  return {
    code: 1,
    data: adjustedStats,
    message: '获取成功'
  }
}

// Mock图表数据API
export const getMockDashboardChartData = async (params?: {
  advertiserId?: number
  accountId?: number
  days?: string
}): Promise<{ code: number; data: ChartData; message: string }> => {
  await delay(600) // 模拟网络延时
  
  return {
    code: 1,
    data: mockChartData,
    message: '获取成功'
  }
} 